package com.wanmait.schedule.config;

import com.wanmait.schedule.interceptor.JWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private JWTInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(jwtInterceptor).addPathPatterns("/web/**").excludePathPatterns("/web/login");
//        registry.addInterceptor(jwtInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/login");
        registry.addInterceptor(jwtInterceptor).addPathPatterns("").excludePathPatterns("/webSocket/**");

        //registry.addInterceptor(jwtInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/login");
    }

    //跨域资源共享
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
        //.allowedOrigins("https://www.wanmait.com","http://localhost:8888")
        .allowedOrigins("*")
                //.allowedOriginPatterns("*")
                //.allowCredentials(true)
        //.allowedMethods("GET","POST","OPTIONS")
        .allowedMethods("*")
        .allowedHeaders("*")
        .maxAge(3600);//3600秒，一个小时内不用再发送预检请求，可以缓存预检请求结果
    }

    //设置静态资源的处理，设置请求路径和静态资源的存放路径的对应关系
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // /static/uploadImages/1.jpg=> src/main/resources/static/uploadImages/1.jpg
        registry.addResourceHandler("/static/uploadImages/**").addResourceLocations("classpath:/static/uploadImages/");
        //  /static/uploadImages/1.jpg=> D:/uploadImages/1.jpg
        //registry.addResourceHandler("/static/uploadImages/**").addResourceLocations("file:D:/uploadImages/");
    }
}
